package ygflower.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import ygflower.dao.FlowerDao;
import ygflower.domain.Flower;
import ygflower.domain.User;

public class FlowerUploadController extends FileUploadController{
	public static final String folder = "photo";
	FlowerDao flowerDao;
    public void setFlowerDao(FlowerDao flowerDao) {
        this.flowerDao = flowerDao;
    }
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(isFormSubmission(request)){
		setFolder(folder);
		setParameterFile(Flower.PropertyPicture);
		
		parseUploadFile(request, response);
		Flower flower = (Flower) bindRequest(request);
		
		Map<String,String> errors = validatorUpload(request,flower);
		if(errors.size() > 0){
			return new ModelAndView(getFormView()).addObject(ErrorMessages, errors);
		}else{
			flowerDao.save(flower);
			return new ModelAndView(getSuccessView()).addObject(FlowerDetailController.viewFlower, flower);
		}
		}else {
			// New form to show: render form view.
			return showNewForm(request, response);
		}
	}


	private Object bindRequest(HttpServletRequest request) {
		Flower flower = new Flower();
		String name = request.getParameter(Flower.PropertyName);
		String marketPrice = request.getParameter(Flower.PropertyMarketPrice);
		String memberPrice = request.getParameter(Flower.PropertyMemberPrice);
		String description = request.getParameter(Flower.PropertyDescription);
		String picture = (String)request.getAttribute(FileName);
		User user = (User) request.getSession().getAttribute(User.CLASSNAME);
		if(StringUtils.isNotBlank(name)){
			flower.setName(name);
		}
		if(StringUtils.isNotEmpty(marketPrice)){
			flower.setMarketPrice(Integer.parseInt(marketPrice));
		}
		if(StringUtils.isNotEmpty(memberPrice)){
			flower.setMemberPrice(Integer.parseInt(memberPrice));
		}
		if(StringUtils.isNotBlank(description)){
			flower.setDescription(description);
		}	
		if(StringUtils.isNotBlank(picture)){
			flower.setPicture(picture);
		}
		if(user != null){
			flower.setUser(user);
		}
		return flower;
	}


	@Override
	Map<String,String> validatorUpload(HttpServletRequest request, Object command) {
		Flower flower = (Flower) command;
		Map<String,String> errors = new HashMap<String, String>();
		if(StringUtils.isEmpty(flower.getName())){
			errors.put(Flower.PropertyName, "必须输入花的名称");
		}
		if(StringUtils.isEmpty(flower.getDescription())){
			errors.put(Flower.PropertyDescription, "必须输入花的信息");
		}
		if(flower.getMarketPrice() == 0){
			errors.put(Flower.PropertyMarketPrice, "必须输入花的市场价格");
		}
		if(StringUtils.isEmpty(flower.getPicture())){
			errors.put(Flower.PropertyPicture, "必须上传一张花的图片");
		}
		if(flower.getUser() == null){
			errors.put(Flower.PropertyUser, "你没有上传的权限");
		}
		return errors;
	}
	

}
